import Actions from './actions';
import DB from '../../app/db';
import {Toast} from 'saltui';
import Reflux from 'reflux';
import nattyStorage from 'natty-storage';

const {fromJS} = require('immutable');

const storageUser = nattyStorage({
  type: 'localStorage',
  key: 'sskj-dingtalk-user',
});


module.exports = Reflux.createStore({
  listenables: [Actions],
  data: {
    user: fromJS(storageUser),
    searchItems: [],
  },

  onEngSearchClear: function () {
    let t = this;
    t.data.searchItems = [];
    t.updateComponent();
  },


  onEngSearch: function (params) {
    let t = this;

    DB.eng.get.search({
      page: 0,
      size: 20,
      keyword: params,
      jdfzr: t.data.user.get('name') ? t.data.user.get('name') : '',
    }).then(
      function (content) {
        t.data.searchItems = content;
        t.updateComponent();
      }, function (err) {
        alert('搜索失败!' + err.errorMessage);
      });
  },


  updateComponent: function () {
    this.trigger(this.data);
  },

  getInitialState: function () {
    return this.data;
  }
});
